package com.dev.module_zqc_sleep.utils

import android.icu.text.SimpleDateFormat
import java.text.ParseException
import java.util.Locale

object AlarmUtils {

    fun calculateSleepDuration(currentTime: String, selectedTime: String): String {
        val formatter = SimpleDateFormat("HH:mm", Locale.getDefault())
        return try {
            val now = formatter.parse(currentTime) ?: return "0小时00分钟"
            val selected = formatter.parse(selectedTime) ?: return "0小时00分钟"

            var duration = selected.time - now.time
            if (duration < 0) {
                // 如果计算结果为负数，则表示跨越了午夜
                duration += 24 * 60 * 60 * 1000 // 加上一天的毫秒数
            }

            // 将毫秒转换为小时和分钟
            val hours = duration / (60 * 60 * 1000)
            val minutes = (duration % (60 * 60 * 1000)) / (60 * 1000)

            "${hours}小时${minutes}分钟"
        } catch (e: ParseException) {
            "0小时00分钟"
        }
    }

}